package com.lg.erp.mapper.chenximou;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.chenximou.Order;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface OrderMapper extends BaseMapper<Order> {
    @Select("select t_order.*,t_product.product_name,customer.`name`,t_product.product_kind,t_order_status.all_status from t_order,t_product,customer,t_order_status\n" +
            "where t_order.product_id=t_product.id \n" +
            "and customer.id=t_order.custom_id\n" +
            "and t_order.order_status=t_order_status.id and t_order.order_status like CONCAT('%',#{key},'%')")
    IPage<Order> selectOrderByPage(Page<Order> page,String key);
    
    @Delete("delete from t_order where id=#{id}")
    boolean removeOrderById(String id);
    
    @Insert("insert into t_order (id,order_id,order_type,product_id,custom_id,pay_type,money,deal_time,number,order_status,explains,supplier_id) value(#{id},#{orderId},#{orderType},#{productName}," +
            "#{name},#{payType},#{money},#{dealTime,jdbcType=TIMESTAMP},#{number},#{orderStatus},#{explains},#{supplierId})")
    boolean saveOrder(Order order);
    
    @Update("update t_order set order_id=#{orderId},order_type=#{orderType},product_id=#{productId},custom_id=#{customId},pay_type=#{payType},money=#{money},deal_time=#{dealTime,jdbcType=TIMESTAMP},number=#{number},order_status=#{orderStatus},explains=#{explains} " +
            "where id=#{id}")
    boolean updateOrder(Order order);

    @Select({"<script>",
            "select t_order.*,t_product.product_name,customer.`name`,t_product.product_kind from t_order,t_product,customer\n" +
            "where 1=1",
            "and t_order.product_id=t_product.id \n" ,
            "and customer.id=t_order.custom_id\n" , 
            "<if test='#{orderId!=null}'>",
            "and t_order.order_id like '%${orderId}%'",
            "</if>",
            "<if test='#{dateValue!=null}'>",
            "and t_order.deal_time like '%${dateValue}%'",
            "</if>",
            "</script>"})
    List<Order> selectOrderByConditions(String orderId,String dateValue);

    @Select("select t_order.*,t_product.product_name,customer.`name`,t_product.product_kind,t_order_status.all_status from t_order,t_product,customer,t_order_status\n" +
            "where t_order.product_id=t_product.id \n" +
            "and customer.id=t_order.custom_id\n" +
            "and t_order.order_status=t_order_status.id")
    List<Order> selectAllOrder();
    
    
    @Select("select t_order.*,t_product.product_name,customer.`name`,t_product.product_kind,t_order_status.all_status from t_order,t_product,customer,t_order_status\n" +
            "where t_order.product_id=t_product.id \n" +
            "and customer.id=t_order.custom_id\n" +
            "and t_order.order_status=t_order_status.id\n" +
            "and t_order.order_status=#{orderStatus}")
    List<Order> selectOrderByStatus(Order order);

    @Select("select t_order.*,t_product.product_name,customer.`name`,t_product.product_kind,t_order_status.all_status from t_order,t_product,customer,t_order_status\n" +
            "where t_order.product_id=t_product.id \n" +
            "and customer.id=t_order.custom_id\n" +
            "and t_order.order_status=t_order_status.id\n" +
            "and t_order.order_status=#{allstatus}")
    List<Order> selectOrderByStatus2(Order order);

    @Select("select  t_order.*,t_production_line.production_name,t_order_status.all_status from t_order,t_production_line,t_order_status\n" +
            "where t_order.product_line_id=t_production_line.id\n" +
            "and  t_order.order_status=t_order_status.id\n" +
            "and t_order.order_status in(#{num1},#{num2})  and t_order.order_status like CONCAT('%',#{key},'%')")
    IPage<Order> selectOrderByStatusPage(Page<Order> page,String num1,String num2,String key);

    @Update("update t_order set order_status=\"6\" \n" +
            "where id=#{id}")
    boolean updateOrderStatusById(Order order);
}
